package com.jfm.jfmc.core.icc.worker;

import com.jfm.jfmc.core.icc.CollectTaskManage;
import com.jfm.jfmm.collector.entity.collect.CollectParam;
import com.jfm.jfmm.collector.entity.collect.CollectResult;
import com.jfm.jfmm.utils.JavaScriptUtil;
import lombok.extern.slf4j.Slf4j;

import javax.script.ScriptException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author DYang
 * @since 2024-10-28 22:11
 *
 * 采集结果采集器
 */
@Slf4j
public class ResultCalculator {

    /**
     * 缓存被依赖的采集项数据
     */
    private static final Map<String, CollectResult> collectResultMap = new ConcurrentHashMap<>();
    /**
     * 依赖列表
     */
    private static final List<String> dependencies = new ArrayList<>();

    /**
     * 添加依赖项
     * @param metricId 采集ID
     */
    public static void addDependId(String metricId){
        if (isDependency(metricId)) {
            return;
        }
        dependencies.add(metricId);
    }


    /**
     * 添加采集结果
     * @param collectResult 采集结果
     */
    public static void addCollectResult(CollectResult collectResult){
        String metricId = collectResult.getResult().getMetricId();
        if (isDependency(metricId)) {
            collectResultMap.put(metricId,collectResult);
        }
    }

    /**
     * 根据依赖项计算
     * @param metricId 采集id
     */
    public static Object calculate(String metricId){
        try {
            // 获取计算函数
            CollectParam collectParam = CollectTaskManage.getCollectParam(metricId);
            if (collectParam == null || collectParam.getParam() == null) {
                return null;
            }
            String funJs = collectParam.getParam().getJsFunc();
            if (funJs == null) {
                return null;
            }
            String[] dependIds = collectParam.getParam().getDependIds();
            if (dependIds == null || dependIds.length == 0) {
                // 直接计算
                return JavaScriptUtil.execute(funJs);
            }else {
                int count = 0;
                for (String dependId : dependIds) {
                    CollectResult collectResult = collectResultMap.get(dependId);
                    if (collectResult == null) {
                        return null;
                    }
                    count++;
                    String code = collectResult.getResult().getMetricCode();
//                    String value = collectResult.getResult().getValue();
//                    funJs = funJs.replace(code,value);
                }
                if (count == dependIds.length) {
                    // 执行计算
                    return JavaScriptUtil.execute(funJs);
                }
            }
        } catch (ScriptException e) {
            log.error("计算依赖项异常",e);
        }
        return null;
    }

    /**
     * 判断是否依赖
     * @param metricId 采集ID
     * @return
     */
    private static boolean isDependency(String metricId){
        return dependencies.contains(metricId);
    }
}
